 

package com.zdkj.subway.rca.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.constant.CommonConstants;
import com.zdkj.common.core.util.R;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.subway.rca.entity.SwCkOthenYjzpManagement;
import com.zdkj.subway.rca.entity.SwCkOthenYjzpManagementLog;
import com.zdkj.subway.rca.service.SwCkOthenYjzpManagementLogService;
import com.zdkj.subway.rca.service.SwCkOthenYjzpManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;


/**
 * 报表调账-其他运营商应急纸票导入日志表管理
 *
 * @author stw
 * @date 2020-12-31 19:38:11
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swckothenyjzpmanagementlog" )
@Api(value = "swckothenyjzpmanagementlog", tags = "报表调账-其他运营商应急纸票导入日志表管理")
public class SwCkOthenYjzpManagementLogController {

    private final SwCkOthenYjzpManagementLogService swCkOthenYjzpManagementLogService;

    @Autowired
    private final SwCkOthenYjzpManagementService swCkOthenYjzpManagementService;
    
    /**
     * 分页查询
     *
     * @param page 分页对象
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getSwCkOthenYjzpManagementLogPage(Page page, String lineCode, String reportType, String beginTime, String endTime) {
        SwCkOthenYjzpManagementLog swCkOthenYjzpManagementLog = new SwCkOthenYjzpManagementLog();
//        if(StringUtils.isNotEmpty(beginTime)){
//            swCkOthenYjzpManagementLog.setBeginTime(LocalDate.parse(beginTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//        }
//        if(StringUtils.isNotEmpty(endTime)){
//            swCkOthenYjzpManagementLog.setEndTime(LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//        }

        return R.ok(swCkOthenYjzpManagementLogService.page(page, Wrappers.query(swCkOthenYjzpManagementLog).lambda()
                .ge(StringUtils.isNotEmpty(beginTime), SwCkOthenYjzpManagementLog::getBeginTime, beginTime)
                .le(StringUtils.isNotEmpty(endTime), SwCkOthenYjzpManagementLog::getEndTime, endTime)
                .eq(StringUtils.isNotEmpty(lineCode), SwCkOthenYjzpManagementLog::getLineCode, lineCode)
                .eq(StringUtils.isNotEmpty(reportType), SwCkOthenYjzpManagementLog::getReportType, reportType)
                .eq(SwCkOthenYjzpManagementLog::getDelFlag,"0")
                .groupBy(SwCkOthenYjzpManagementLog::getBeginTime,SwCkOthenYjzpManagementLog::getEndTime,
                        SwCkOthenYjzpManagementLog::getReportType,SwCkOthenYjzpManagementLog::getProjectItem,
                        SwCkOthenYjzpManagementLog::getLineCode)
                .orderByDesc(SwCkOthenYjzpManagementLog::getCreateTime)
        ));
    }

    /**
     * 误出站应急纸票导出模板
     * @param
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "误出站应急纸票导出模板", notes = "误出站应急纸票导出模板")
    @GetMapping("/exportTamplateWcz")
    @PreAuthorize("@pms.hasPermission('sw_ck_othen_yjzp_management_import')")
    public void exportTamplateWcz(HttpServletResponse response) {
        swCkOthenYjzpManagementLogService.exportTamplateWcz(response);
    }

    /**
     * 大客流应急纸票导出模板
     * @param
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "大客流应急纸票导出模板", notes = "大客流应急纸票导出模板")
    @GetMapping("/exportTamplateDkl")
    @PreAuthorize("@pms.hasPermission('sw_ck_othen_yjzp_management_import')")
    public void exportTamplateDkl(HttpServletResponse response) {
        swCkOthenYjzpManagementLogService.exportTamplateDkl(response);
    }

    @ApiOperation(value = "导入", notes = "导入")
    @SysLog("导入其他运营商应急纸票")
    @PostMapping(value = "/saveBatch", headers = "content-type=multipart/form-data")
    @PreAuthorize("@pms.hasPermission('sw_ck_othen_yjzp_management_import')")
    public R saveBatch(@RequestParam(value = "file") MultipartFile file,String reportType) throws Exception {
        return R.ok((Object) this.swCkOthenYjzpManagementLogService.saveBatchWithExcel(file));
    }

    /**
     * 浏览
     * @return
     */
    @ApiOperation(value = "浏览，（id:浏览的那条数据的ID）", notes = "浏览，（id：浏览的那条数据的ID）")
    @GetMapping("/listPreview" )
    @PreAuthorize("@pms.hasPermission('sw_ck_othen_yjzp_management_import')")
    public R listPreview(Long id) throws Exception {
        if(id == null){
            return R.ok(CommonConstants.FAIL);
        }

        SwCkOthenYjzpManagementLog logObj =  this.swCkOthenYjzpManagementLogService.getById(id);
        SwCkOthenYjzpManagement swCkOthenYjzpManagement = new SwCkOthenYjzpManagement();
        //swCkOthenYjzpManagement.setLogId(logObj.getUuid());
        //List<SwCkOthenYjzpManagement> list = swCkOthenYjzpManagementService.list(Wrappers.query(swCkOthenYjzpManagement));

       
		String startDate =  logObj.getBeginTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));		
		String endDate =    logObj.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

		QueryWrapper<SwCkOthenYjzpManagement> queryWrapper = new QueryWrapper<SwCkOthenYjzpManagement>();
        queryWrapper.eq("LINE_CODE",logObj.getLineCode());
        queryWrapper.eq("REPORT_TYPE",logObj.getReportType());
        queryWrapper.eq("PROJECT_ITEM",logObj.getProjectItem());
        queryWrapper.between("statistics_date", startDate, endDate);

        //swCkOthenTicketRevenueManagement.setLogId(logObj.getUuid());
		List<SwCkOthenYjzpManagement> list = swCkOthenYjzpManagementService.list(queryWrapper);	
		  

        if(list != null && !list.isEmpty()){
            List<Map<String,String>> projectItemMap = swCkOthenYjzpManagementService.selectProjectItemMap();
            for(SwCkOthenYjzpManagement obj : list){
                obj.setProjectItem(getProjectItemName(projectItemMap,obj.getProjectItem()));
            }
        }

        return R.ok(list);

    }
    
    /**
     * 获取项目名称
     * @param projectItemMap
     * @param projectItem
     * @return
     */
    private String getProjectItemName (List<Map<String,String>> projectItemMap,String projectItem) {
        for (Map<String,String> map : projectItemMap){
            if(projectItem.equals(map.get("value"))){
                return map.get("label");
            }
        }
        return "";
    }
}
